﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Chess
{
    public class Piece: InterfacePiece
    {
        public InterfacePiece iPiece;
        public Player player;
        public Square square;
        public enum Name{Pawn, Rook, Knight, Bishop, Queen, King}
           
        public Square Square
        {
            get { return square; }
            set { square = value; }
        }

        public int ImageIndex
        {
            get {return iPiece.ImageIndex;}
        }
        public Player Player
        {
            get {return player;}
        }

        public Piece(Piece.Name name,Player player,int file,int rank)
        {
            Square square = Board.GetSquare(file,rank);
            this.player = player;
            this.Square = square;
            square.Piece = this;         

            switch (name)
            {
                case Piece.Name.Pawn:
                    iPiece = new Pawn(this);
                    break;

                case Piece.Name.Rook:
                    iPiece = new Rook(this);
                    break;

                case Piece.Name.Knight:
                    iPiece = new Knight(this);
                    break;

                case Piece.Name.Bishop:
                    iPiece = new Bishop(this);
                    break;              
               
                case Piece.Name.Queen:
                    iPiece = new Queen(this);
                    break;

                case Piece.Name.King:
                    iPiece = new King(this);
                    break;
            }
        }
      
        public void SetPosition(Moves moves)
        {
            iPiece.SetPosition(moves);
        }

        public void ValidMove(Moves moves)
        {
            SetPosition(moves);
            Move move;
            for (int index = moves.MoveNumber - 1; index >= 0; index--)
            {
                move = moves.Item(index);
                Move moveUndo = move.Piece.Move(move.To);
                Chess.Move.Undo(moveUndo);
            }
        }          

        public Move Move(Square square)
        {
            Square squarePiecePlay = square;
            Move move = new Move(this, this.square, square, squarePiecePlay.Piece);
            this.square.Piece = null;
            square.Piece = this;
            this.square = square;     
            Game.History.Add(move);
            return move;
        }       		
    }
}
